import { _decorator, Component, Node, Button, director, AudioClip } from 'cc';
import { eventCenter } from '../Utils/JJWXR_EventCenter';
import { JJWXR_Events } from '../Utils/JJWXR_Events';
import { JJWXR_EnergyManager } from './JJWXR_EnergyManager';
import { AudioManager } from '../Utils/JJWXR_AudioManager';
import { LevelData } from './JJWXR_Loading';
import { ProjectEvent, ProjectEventManager } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;


@ccclass('JJWXR_MenuManager')
export class JJWXR_MenuManager extends Component {

    @property(Button)
    private btnSetting: Button = null;    // 设置按钮

    @property(AudioClip)
    private backgroundMusic: AudioClip = null; // 点击音效
    // -------------------------------------------------------------------------------------------------------------------------------

    @property(Button)
    private btnGameStart: Button = null;  // 游戏开始按钮
    @property(Button)
    private btnArmory: Button = null; // 装备按钮
    // -------------------------------------------------------------------------------------------------------------------------------
    @property(Node)
    private settingUI: Node = null; // 设置界面
    @property(Node)
    private addEnergyUI: Node = null; // 添加能量界面
    @property(Node)
    private armoryUI: Node = null; // 装备界面
    @property(Node)
    private warningUI: Node = null; // 警告界面
    // -------------------------------------------------------------------------------------------------------------------------------

    // -------------------------------------------------------------------------------------------------------------------------------
    // 单例模式
    private static _instance: JJWXR_MenuManager = null;
    public static get instance(): JJWXR_MenuManager {
        return this._instance;
    }

    onLoad() {
        JJWXR_MenuManager._instance = this;
        this.btnSetting.node.on(Button.EventType.CLICK, this.onSetting, this);
        this.btnGameStart.node.on(Button.EventType.CLICK, this.onGameStart, this);
        this.btnArmory.node.on(Button.EventType.CLICK, this.onArmory, this);
        ProjectEventManager.emit(ProjectEvent.返回主页);
    }

    start() {
        const isFirstPlay = JSON.parse(localStorage.getItem('isFirstPlay'));
        const currentLevel = JSON.parse(localStorage.getItem('currentLevel'));
        // if (isFirstPlay && currentLevel == 1) {
        //     console.log('第一次进入游戏MenuManager' + isFirstPlay + currentLevel);
        //     director.loadScene('JJWXR_LoadingScene'); // 第一次进入游戏，加载加载场景
        // }
        // else {
        //     let isFirstPlay = JSON.parse(localStorage.getItem('isFirstPlay'));
        //     const isPlay = false;
        //     localStorage.setItem('isFirstPlay', JSON.stringify(isPlay));
        // }

        // 初始化菜单界面
        this.hideSettingUI();
        this.hideAddEnergyUI();
        this.hideArmoryUI();

        this.playBackgroundMusic();

        eventCenter.on(JJWXR_Events.SHOW_ADD_ENERGY_UI, this.showAddEnergyUI, this);
        eventCenter.on(JJWXR_Events.HIDE_ADD_ENERGY_UI, this.hideAddEnergyUI, this);
    }

    onDestroy() {
        eventCenter.off(JJWXR_Events.SHOW_ADD_ENERGY_UI, this.showAddEnergyUI, this);
        eventCenter.off(JJWXR_Events.HIDE_ADD_ENERGY_UI, this.hideAddEnergyUI, this);

        // 停止背景音乐
        AudioManager.instance.stop();
    }

    // 设置按钮
    onSetting() {
        console.log('跳转到设置界面');
        // 跳转到设置界面
        this.showSettingUI();
    }

    // 播放背景音乐
    playBackgroundMusic() {
        // 播放背景音乐
        let isPlayBGM = JSON.parse(localStorage.getItem('setting'));
        AudioManager.instance.play(this.backgroundMusic, isPlayBGM.sound);
    }

    // 添加时间按钮
    onAddEnergy() {
        console.log('跳转到添加时间界面');
        // 跳转到添加时间界面
        this.showAddEnergyUI();
    }

    // 游戏开始按钮
    onGameStart() {
        console.log('跳转到游戏界面');
        // 判断是否可以开始游戏
        if (JJWXR_EnergyManager.instance.getEnergyNum() <= 0) {
            // 显示警告界面
            this.showWarningUI();
            return;
        }
        // 消耗能量
        eventCenter.emit(JJWXR_Events.USE_ENERGY);

        // 跳转到加载游戏界面
        director.loadScene('JJWXR_LoadingScene');
    }

    // 装备按钮
    onArmory() {
        console.log('跳转到装备界面');
        // 跳转到装备界面
        this.showArmoryUI();
    }

    // 显示设置界面
    showSettingUI() {
        this.settingUI.active = true;
    }
    // 隐藏设置界面
    hideSettingUI() {
        this.settingUI.active = false;
    }

    // 显示添加能量界面
    showAddEnergyUI() {
        this.addEnergyUI.active = true;
    }
    // 隐藏添加能量界面
    hideAddEnergyUI() {
        this.addEnergyUI.active = false;
    }

    // 显示装备界面
    showArmoryUI() {
        this.armoryUI.active = true;
    }
    // 隐藏装备界面
    hideArmoryUI() {
        this.armoryUI.active = false;
    }

    // 显示警告界面
    showWarningUI() {
        this.warningUI.active = true;
        this.scheduleOnce(() => {
            this.hideWarningUI();
        }, 2);
    }
    // 隐藏警告界面
    hideWarningUI() {
        this.warningUI.active = false;
    }
}